﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.Core.Helper;

namespace Chinaer.Bll
{
    public class Employee : Repository, IBll.IEmployee
    {
        IDao.IEmployeeDAO employeeDAO = new Dao.EmployeeDAO();
        public List<DTO.EmployeeDTO> GetEmployeesByDianPuID(string dianPuID)
        {
            List<DTO.EmployeeDTO> listEmployeeDTO = new List<DTO.EmployeeDTO>();
            try
            {
                List<Model.Entities.Model_Employee> listModelEmployee = new List<Model.Entities.Model_Employee>();
                listModelEmployee = employeeDAO.GetAllEmployeesByDianPuID(dianPuID);
                if (listModelEmployee != null)
                {
                    foreach (var item in listModelEmployee)
                    {
                        DTO.EmployeeDTO employeeDTO = new DTO.EmployeeDTO();
                        Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(item, employeeDTO);
                        listEmployeeDTO.Add(employeeDTO);
                    }
                }
            }
            catch (Exception ex)
            {
                listEmployeeDTO = null;
            }
            return listEmployeeDTO;
        }

        public Model.Common.JsonResult Add(DTO.EmployeeDTO t)
        {
            Model.Common.JsonResult jsonResult = new Model.Common.JsonResult();
            try
            {
                Model.Entities.Model_Employee model_Employee = new Model.Entities.Model_Employee();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_Employee);
                jsonResult.Success = employeeDAO.Add(model_Employee);
            }
            catch (Exception ex)
            {
                jsonResult = ex.ExceptionJsonResult();
            }
            return jsonResult;
        }

        public Model.Common.JsonResult Delete(DTO.EmployeeDTO t)
        {
            Model.Common.JsonResult jsonResult = new Model.Common.JsonResult();
            try
            {
                Model.Entities.Model_Employee model_Employee = new Model.Entities.Model_Employee();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_Employee);
                jsonResult.Success = employeeDAO.Delete(model_Employee);
            }
            catch (Exception ex)
            {
                jsonResult = ex.ExceptionJsonResult();
            }
            return jsonResult;
        }

        public Model.Common.JsonResult Delete(string id)
        {
            Model.Common.JsonResult jsonResult = new Model.Common.JsonResult();
            try
            {
                jsonResult.Success = employeeDAO.Delete(id);
            }
            catch (Exception ex)
            {
                jsonResult = ex.ExceptionJsonResult();
            }
            return jsonResult;
        }

        public Model.Common.JsonResult Update(DTO.EmployeeDTO t)
        {
            Model.Common.JsonResult jsonResult = new Model.Common.JsonResult();
            try
            {
                Model.Entities.Model_Employee model_Employee = new Model.Entities.Model_Employee();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_Employee);
                jsonResult.Success = employeeDAO.Update(model_Employee);
            }
            catch (Exception ex)
            {
                jsonResult = ex.ExceptionJsonResult();
            }
            return jsonResult;
        }

        public DTO.EmployeeDTO GetByID(string id)
        {
            DTO.EmployeeDTO employeeDTO = new DTO.EmployeeDTO();
            try
            {
                Model.Entities.Model_Employee model_Employee = new Model.Entities.Model_Employee();
                model_Employee = employeeDAO.GetByID(id);
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_Employee, employeeDTO);
            }
            catch (Exception ex)
            {
                employeeDTO = null;
            }
            return employeeDTO;
        }
    }
}
